package com.example.springlog;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Description:
 * @Author: 刘超
 * @Date: 2023-06-06 16:57
 */
public class Filedtest {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Person p = new Person();
        Field field = p.getClass().getField("name");

        boolean flag = true;
        try {
            PropertyDescriptor descriptor = new PropertyDescriptor(field.getName(), p.getClass());
            Method writeMethod = descriptor.getWriteMethod();
            writeMethod.invoke(p, "wangwu");
            flag = false;
        } catch (Exception e) {

        }
        if (flag) {
            field.set(p, "zhangsan");
        }
        System.out.println(p);

    }
}
